package cn.south.campus.admin.sys.service.impl;

import cn.south.campus.admin.config.TokenConfig;
import cn.south.campus.admin.sys.model.bo.LoginContext;
import cn.south.campus.admin.sys.model.bo.LoginInfo;
import cn.south.campus.admin.sys.model.enums.EnableEnums;
import cn.south.campus.admin.sys.model.req.LoginReq;
import cn.south.campus.admin.sys.model.vo.LoginVO;
import cn.south.campus.admin.sys.model.vo.SysRoleVo;
import cn.south.campus.admin.sys.model.vo.SysUserMenuVo;
import cn.south.campus.admin.sys.model.vo.SysUserVo;
import cn.south.campus.admin.sys.service.LoginService;
import cn.south.campus.admin.sys.service.SysMenuService;
import cn.south.campus.admin.sys.service.SysRoleService;
import cn.south.campus.admin.sys.service.SysUserService;
import cn.south.campus.common.cache.CacheService;
import cn.south.campus.common.model.enums.CommonEnums;
import cn.south.campus.common.utils.AssertUtils;
import cn.south.campus.common.utils.GenSerializeUtil;
import cn.south.campus.common.utils.LoggerUtil;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author chow
 * @since 2023/3/14 9:38
 */
@Service
@AllArgsConstructor
public class LoginServiceImpl implements LoginService {

    private final Logger LOG = LoggerFactory.getLogger(LoginServiceImpl.class);

    private CacheService cacheService;

    private SysUserService sysUserService;

    private SysMenuService sysMenuService;

    private SysRoleService sysRoleService;


    @Override
    public String login(LoginReq req) {
        SysUserVo sysUserVo = sysUserService.selectByMobileAndPwd(req.getMobile(), req.getPassword());
        AssertUtils.isNotNull(sysUserVo, CommonEnums.PHONE_OR_PWD_ERROR);
        AssertUtils.isNotEquals(EnableEnums.DISABLE.getCode(), sysUserVo.getLocked(), CommonEnums.USER_IS_LOCKED_ERROR);
        String token = GenSerializeUtil.genToken();
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setToken(token);
        loginInfo.setId(sysUserVo.getId());
        loginInfo.setUsername(sysUserVo.getUsername());
        loginInfo.setMobile(sysUserVo.getMobile());
        loginInfo.setEmail(sysUserVo.getEmail());
        loginInfo.setAvatar(sysUserVo.getAvatar());
        LoginContext.setContext(loginInfo);
        cacheService.setCacheObject(token, loginInfo, TokenConfig.EXPIRE);
        return token;
    }

    @Override
    public void logout() {
        LoginInfo context = LoginContext.getContext();
        if (context == null) {
            return;
        }
        boolean deleteObject = cacheService.deleteObject(context.getToken());
        if (!deleteObject) {
            LoggerUtil.error(LOG, "登出失败{0}", context.getMobile());
        }
        LoginContext.remove();
    }

    @Override
    public LoginVO userInfo() {
        LoginInfo context = LoginContext.getContext();
        SysUserVo sysUserVo = sysUserService.detail(context.getId());
        AssertUtils.isNotNull(sysUserVo, CommonEnums.LOGIN_EXPIRE);
        Long roleId = sysUserVo.getRoleId();
        List<SysUserMenuVo> menuRole = sysMenuService.findMenuRole(roleId);
        SysRoleVo sysRoleVo = sysRoleService.detail(roleId);
        LoginVO loginVO = new LoginVO();
        loginVO.setUsername(sysUserVo.getUsername());
        loginVO.setAvatar(sysUserVo.getAvatar());
        loginVO.setMobile(sysUserVo.getMobile());
        loginVO.setEmail(sysUserVo.getEmail());
        loginVO.setRoleId(sysUserVo.getRoleId());
        loginVO.setRoleName(sysRoleVo.getRoleName());
        loginVO.setDeptId(sysUserVo.getDeptId());
        loginVO.setDeptName("");
        loginVO.setMenus(menuRole);
        loginVO.setId(context.getId());
        return loginVO;
    }

    @Override
    public void modifyInfo(LoginVO vo) {
        sysUserService.modifyMyInfo(vo);
        this.logout();
    }
}
